const router = require("express").Router();
const Question = require("../model/question");
const { getLoginUserFromJWT } = require("../util/tools");

//测试用户存取数据
router.post("/addQuestion", async (req, res, next) => {
  try {
    //验证解密用户的id
    const uid = getLoginUserFromJWT(req);
    const question = new Question({
      type: req.body.type,
      content: req.body.content,
      uid: uid,
    });
    const result = await question.save();
    res.json({
      code: 200,
      msg: "添加成功",
      questions: result,
    });
  } catch (err) {
    next(err);
  }
});

//获取用户数据
router.get("/getQuestion", async (req, res, next) => {
  try {
    //验证解密用户的id
    const uid = getLoginUserFromJWT(req);
    const questions = await Question.find({ uid });
    if (questions) {
      res.json({
        code: 200,
        msg: "获取成功",
        questions,
      });
    }
  } catch (err) {
    next(err);
  }
});
module.exports = router;
